﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Share.Log;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.RemoteCtrls
{
    public class TypeProberProxy : MarshalByRefProxyBase
    {
        /// <summary>
        /// 探测活动
        /// </summary>
        /// <param name="searchPaths"></param>
        /// <returns></returns>
        public List<string> ProbeActivities(string[] searchPaths)
        {
            List<string> activities = new List<string>();

            foreach (var path in searchPaths)
            {
                if (Directory.Exists(path))
                {
                    string[] filePaths = Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories);
                    if (filePaths != null)
                    {
                        Type activityType = typeof(System.Activities.Activity);
                        foreach (string filePath in filePaths)
                        {
                            try
                            {
                                var assembly = Assembly.LoadFile(filePath);
                                var isContain = assembly.GetExportedTypes()
                                    .Any(type => type.IsSubclassOf(activityType));

                                if (isContain)
                                {
                                    activities.Add(filePath);
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }

            return activities;
        }

        public List<string> ProbeAssemblies(string[] searchPaths)
        {
            List<string> assemblies = new List<string>();

            foreach (var path in searchPaths)
            {
                if (Directory.Exists(path))
                {
                    string[] filePaths = Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories);

                    if (filePaths != null)
                    {
                        foreach (string filePath in filePaths)
                        {
                            try
                            {
                                var assembly = Assembly.LoadFile(filePath);
                                var typeCount = assembly.ExportedTypes.Count();
                                if (typeCount > 0)//判断是否有公开类型
                                {
                                    assemblies.Add(assembly.FullName);
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }

            return assemblies;
        }
    }
}
